{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "65f38648",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.00000000e+00  2.00000000e+00  3.00000000e+00  4.00000000e+00\n",
      "   5.00000000e+00]\n",
      " [ 0.00000000e+00 -7.50000000e-01 -1.50000000e+00 -2.25000000e+00\n",
      "  -3.00000000e+00]\n",
      " [ 0.00000000e+00  0.00000000e+00  1.07142857e-01  2.14285714e-01\n",
      "   2.14285714e-01]\n",
      " [-0.00000000e+00 -3.70074342e-17  0.00000000e+00 -1.25000000e-01\n",
      "  -2.14285714e-01]]\n",
      "[-1.          1.71428571 -1.42857143  1.71428571]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    "matrixA=np.array([[1,2,3,4,5],[4,5,6,7,8],[8,9,11,13,14],[15,16,11,13,19]],dtype=float)\n",
    "\n",
    "def guassElimination(matrixA):    #简单的高斯消元 numpy使用np.linalg.solve(a, b)解线性方程\n",
    "    def elimination(matrixA,row): #定义单次消元函数\n",
    "        totalRow=matrixA.shape[0]\n",
    "        for i in range(row,totalRow):\n",
    "            matrixA[i]=matrixA[i]*matrixA[row-1][row-1]/matrixA[i][row-1]-matrixA[row-1]\n",
    "        return matrixA\n",
    "\n",
    "    for i in range(matrixA.shape[0]-1): \n",
    "        row=i+1\n",
    "        matrixA=elimination(matrixA,row)\n",
    "    return matrixA\n",
    "\n",
    "def solvingLinerEquations(triMatrix):  #由三角矩阵解出x1,x2...xn\n",
    "    row,column=triMatrix.shape\n",
    "    ansMatrix=np.zeros(column-1)  #定义0数组，用来接收解出来的x\n",
    " \n",
    "    def getIMatrix(triMatrix,calRow):#row以下的行已经转化为单位矩阵\n",
    "        totalRow,totalColumn=triMatrix.shape\n",
    "        for i in range(calRow,totalRow-1):\n",
    "            i=i+1   \n",
    "            matrixA[calRow]=matrixA[calRow]-matrixA[calRow][i]*matrixA[i]\n",
    "        triMatrix[calRow]=triMatrix[calRow]/triMatrix[calRow][calRow]\n",
    "        return triMatrix\n",
    "    \n",
    "    for i in range(row): #要由下向上计算，因为最下面一行已经是单元阵\n",
    "        newRow=row-i-1\n",
    "        triMatrix=getIMatrix(triMatrix,newRow)\n",
    "        ansMatrix[newRow]=triMatrix[newRow][column-1]\n",
    "    return ansMatrix\n",
    "        \n",
    "matrixA=guassElimination(matrixA)\n",
    "print(matrixA)\n",
    "a=solvingLinerEquations(matrixA)\n",
    "print(a)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "f6f92450",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.0\n"
     ]
    }
   ],
   "source": [
    "b=np.array([1,2,3,4])\n",
    "print(np.sum(a*b))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
